# 用户-角色绑定接口
from flask import request, jsonify
from flask_jwt_extended import jwt_required
from backend.api.user_api import admin_required
from backend.services.user_role_service import get_user_roles, set_user_roles

@jwt_required()
@admin_required
def list_user_roles(user_id):
    roles = get_user_roles(user_id)
    return jsonify({'success': True, 'data': roles})

@jwt_required()
@admin_required
def update_user_roles(user_id):
    data = request.get_json(force=True, silent=True)
    role_ids = data.get('role_ids', [])
    result = set_user_roles(user_id, role_ids)
    return jsonify(result)
